var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderlist: [],
    currentTabsIndex: 2,
    now_page: 1,
    totalpage:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getdoing(2);
  },
  // 获取进行中数据
  getdoing: function (type) {
    var that = this;
    var shopid = wx.getStorageSync("shop_id");
    var param = {
      url: "api/shop/grabOrders",
      data: {
        shop_id: shopid,
        now_page: 1,
        type: type
      },
      success: function (e) {
        console.log(e)
        var order = e.data.data.list;
        that.setData({
          orderlist: order,
          totalpage:e.data.data.total_page
        })
      },
    };
    app.post(param);
  },
  // tab
  navTap: function (e) {
    if (this.data.currentTabsIndex == e.currentTarget.dataset.index) {
      return false;
    } else {
      this.setData({
        currentTabsIndex: e.currentTarget.dataset.index,
        now_page:1,
        totalpage:0
      })
      this.getdoing(e.currentTarget.dataset.index);
    }
  },
  // 播放录音
  luyinplay: function (e) {
    var myaudio = wx.createInnerAudioContext();
    var src = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var orderlist = this.data.orderlist;
    var playing = orderlist[index].playing;
    if(playing == false){
      myaudio.play();
      playing = true;
      this.setData({
        orderlist:orderlist
      })
      myaudio.onEnded(() => {
        playing = false
        this.setData({
          orderlist: orderlist
        })
      })
    }else{
      myaudio.play();
      playing = false;
      this.setData({
        orderlist: orderlist
      })
    }
    
  },
  // 删除订单
  delit:function(e){
    wx.showModal({
      title: '提醒',
      content: '确定要删除此订单吗？',
      success: function (res) {
        if (res.confirm) {
          this.delorder(e)
        } else if (res.cancel) {
          return false;
        }
      }
    }) 
  },
  delorder:function(e){
    var id = e.currentTarget.dataset.id;
    var param = {
      url:'',
      data:{
        id:id
      },
      success:function(res){
        wx.showToast({
          title: res.data.message,
          icon:'none',
          duration:2000
        })
      }
    };
    app.post(param);
  },
  
  onReachBottom: function () {
    var that = this;
    // 显示加载图标
    
    var now_page = parseInt(that.data.now_page)+1;
    if (now_page > parseInt(that.data.totalpage)){
      wx.showToast({
        title: '没有更多数据了',
        icon:"none",
        duration:2000
      })
      return false;
    }else{
      wx.showLoading({
        title: '玩命加载中',
      })
      var shopid = wx.getStorageSync("shop_id");
      var param = {
        url: "api/user/demandList",
        data: {
          user_id: userid,
          now_page: now_page,
          type: that.data.currentTabsIndex
        },
        success: function (e) {
          that.setData({
            orderlist: that.data.orderlist.concat(e.data.data.list),
            now_page: now_page
          })
          // 隐藏加载框
          wx.hideLoading();

        },
      };
      app.post(param);
    }
    

  },


})